/**
 * 功能描述 卡片
 */

define(function (require, exports, module) {
	var Component = require("form");
	var aw = require("ajaxwrapper");
	var Dialog=require("dialog");
	
    var instance = {
        initComponent : function (params, widget) {
            var component = new Component({
            	 parentNode: ".J-editform",
            	 show : false,
            	 saveaction : function() {
            		 var data=widget.get("editform").getData();
            		 var validity=data.validity;
            		 var liveDays=data.liveDays;
            		 var r = /^(100|[1-9]\d|\d)$/;//0-100的正整数  		 
        			 if(!r.test(validity)){
        				Dialog.alert({
	                		title:"提示",
	                		content:"有效期只能输入非零正整数！"
	                	});
        				widget.get("editform").setValue("validity","");
        				return false;
        			 }else{
        				if(parseInt(validity)==0){
        					Dialog.alert({
		                		title:"提示",
		                		content:"有效期只能输入非零正整数！"
		                	});
	        				widget.get("editform").setValue("validity","");
	        				return false;
        				}
        			 }
        			 if(!r.test(liveDays)){
         				Dialog.alert({
 	                		title:"提示",
 	                		content:"居住期限只能输入非零正整数！"
 	                	});
         				widget.get("editform").setValue("liveDays","");
         				return false;
         			 }else{
         				if(parseInt(liveDays)==0){
         					Dialog.alert({
 		                		title:"提示",
 		                		content:"居住期限只能输入非零正整数！"
 		                	});
 	        				widget.get("editform").setValue("liveDays","");
 	        				return false;
         				}
         			 }
            		 var productType=widget.get("editform").getData("productType");
            		 var productBatch=widget.get("editform").getData("productBatch");
            		 var expenseScope=widget.get("editform").getData("expenseScope");
            		 var bedScope=widget.get("editform").getData("bedScope");
            		 var rebatesScope=widget.get("editform").getData("rebatesScope");
            		 var subsidyScope=widget.get("editform").getData("subsidyScope");
            		 //把data里每一个对象的值从PK转换成对象形式
            		 for(var i in productType){
            			 if(productType[i].pkProductType==parseInt(data.productType)){
            				 data.productType=productType[i];
            			 }
            		 }
            		 for(var i in productBatch){
            			 if(productBatch[i].pkProductBatch==parseInt(data.productBatch)){
            				 data.productBatch=productBatch[i];
            			 }
            		 }
            		 for(var i in expenseScope){
            			 for(var j in data.expenseScope){
            				 if(expenseScope[i].pkServicePoint==parseInt(data.expenseScope[j])){
            					 var servicePoint={};
            					 servicePoint.pkServicePoint=expenseScope[i].pkServicePoint;
            					 servicePoint.name=expenseScope[i].name
                				 data.expenseScope[j]=servicePoint;
                			 }
            			 }
            		 }
            		 for(var i in bedScope){
            			 for(var j in data.bedScope){
            				 if(bedScope[i].pkServicePoint==parseInt(data.bedScope[j])){
            					 var servicePoint={};
            					 servicePoint.pkServicePoint=bedScope[i].pkServicePoint;
            					 servicePoint.name=bedScope[i].name
                				 data.bedScope[j]=servicePoint;
                			 }
            			 }
            		 }
            		 for(var i in rebatesScope){
            			 for(var j in data.rebatesScope){
            				 if(rebatesScope[i].pkServicePoint==parseInt(data.rebatesScope[j])){
            					 var servicePoint={};
            					 servicePoint.pkServicePoint=rebatesScope[i].pkServicePoint;
            					 servicePoint.name=rebatesScope[i].name
                				 data.rebatesScope[j]=servicePoint;
                			 }
            			 }
            		 }
            		 for(var i in subsidyScope){
            			 for(var j in data.subsidyScope){
            				 if(subsidyScope[i].pkServicePoint==parseInt(data.subsidyScope[j])){
            					 var servicePoint={};
            					 servicePoint.pkServicePoint=subsidyScope[i].pkServicePoint;
            					 servicePoint.name=subsidyScope[i].name
                				 data.subsidyScope[j]=servicePoint;
                			 }
            			 }
            		 }
            		//唯一性校验
					aw.ajax({
						url: "api/productdetail/queryonly",
						data:{
							"product.name":widget.get("form").getValue("name"),
							"productType.name":data.productType.name,
							"productBatch.name":data.productBatch.name,
						},
						dataType:"json",
						success:function(dat){
							if(dat.length!=0){
								 if(data.pkProductDetail==""&&widget.get("type")==false){
			            			 var dat=widget.get("editgrid").getData();
			                		 dat.push(data);
			                		 widget.get("editgrid").setData(dat);
			            		 }else{
			            			 var indexs=widget.get("index");
			            			 widget.get("editgrid").update(indexs, data);
			            		 }
			            		 widget.hide(".J-editform").show([".J-form",".J-editgrid"]);
			            		 widget.get("subnav").show(["cancel","save"]);
//			        	         widget.get("subnav").show(["cancel","save"]).hide(["return"]);
			        	         $(".J-button").removeClass("hidden");
							}
						}
					});
            	 },cancelaction:function(){
            		 widget.hide(".J-editform").show([".J-form",".J-editgrid"]);
            		 widget.get("subnav").show(["cancel","save"]);
//        	         widget.get("subnav").show(["cancel","save"]).hide(["return"]);
        	         $(".J-button").removeClass("hidden");
	      		 },
                 model: {
                     id: "editform",
                     layout: "3_1",
                     items: [{
                    	 name : "pkProduct",
                         type : "hidden",
                     },{
                    	 name : "pkProductDetail",
                         type : "hidden",
                     },{
                    	 name : "version",
                         type : "hidden",
                         defaultValue:"0"
                     },{
                    	 name : "productType",
                    	 type : "select",
                         label : "产品类型",
                         url:"api/producttype/query",
						 key:"pkProductType",
						 value:"name",
						 validate:["required"]
                     },{
                    	 name : "productBatch",
                         type : "select",
                         label : "产品批次",
                         url:"api/productbatch/query",
						 key:"pkProductBatch",
						 value:"name",
                     },{
                         name : "price",
                         type : "text",
                         label : "产品价格（元）",
                         validate:["required"]
                     },{
                     	 name : "validity",
                     	 type : "text",
                         label : "有效期（年）",
                         validate:["required"]
                     },{
                    	  name : "liveDays",
                          type : "text",
                          label : "居住期限（天/间）",
                          validate:["required"]
//                     },{
//                    	  name : "",
//                          type : "select",
//                          label : "费用抵扣规则",
//                          url:"",
// 						  key:"",
// 						  value:"name",
                     },{
                    	 name : "expenseScope",
                         type : "select",
                         label : "费用抵扣适用范围",
//                         url:"api/servicepoint/operableservicepoints",
						 key:"pkServicePoint",
						 value:"name",
						 multi: true
                     },{
                    	 name : "discount",
                         type : "text",
                         label : "床位费折扣",
                     },{
                    	 name : "bedScope",
                         type : "select",
                         label : "床位费折扣适用范围",
//                         url:"api/servicepoint/operableservicepoints",
						 key:"pkServicePoint",
						 value:"name",
						 multi: true
//                     },{
//                    	 name : "",
//                         type : "select",
//                         label : "返现规则",
//                         url:"",
//						 key:"",
//						 value:"name",
                     },{
                    	 name : "rebatesScope",
                         type : "select",
                         label : "返现适用范围",
//                         url:"api/servicepoint/operableservicepoints",
						 key:"pkServicePoint",
						 value:"name",
						 multi: true
//                     },{
//                    	 name : "",
//                         type : "select",
//                         label : "补贴规则",
//                         url:"",
//						 key:"",
//						 value:"name",
                     },{
                    	 name : "subsidyScope",
                         type : "select",
                         label : "补贴适用范围",
//                         url:"api/servicepoint/operableservicepoints",
						 key:"pkServicePoint",
						 value:"name",
						 multi: true
                     },{
                    	 name:"description",
  						 type:"textarea",
  						 label:"备注",
  						 "break":true
                     }]
                 }
            });
            widget.set("editform", component);
        }
    };
    module.exports = instance;
});
